<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <title>技术论坛系统</title>
    <link href="/css/bootstrap.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">




</head>
<body style="background-color: #fafafa">
<script type="text/javascript" src="/js/jquery-1.11.3.js"></script>
<script type="text/javascript" src="/js/bootstrap.js"></script>

<div class="container-fluid">
    <img src="/img/navback.jpg" alt="..." class="note-img">
    <div class="row">
        <div class="col-md-3">
            <ul class="list-group">
                <li class="list-group-item active">分类管理</li>
                <li class="list-group-item"><a href="${pageContext.request.contextPath}/api/admin/manageNote">帖子管理</a></li>

            </ul>

        </div>

        <div class="col-md-9 manage-model">
            <div>

                <!-- Nav tabs -->
                <ul class="nav nav-tabs" role="tablist">
                    <li role="presentation" class="active"><a href="#createTags" aria-controls="createTags" role="tab" data-toggle="tab">创建板块</a></li>
                    <li role="presentation"><a href="#createTypes" aria-controls="createTypes" role="tab" data-toggle="tab">创建分类</a></li>
                    <li role="presentation"><a href="#editTag" aria-controls="editTag" role="tab" data-toggle="tab">编辑/删除板块</a></li>
                    <li role="presentation"><a href="#editType" aria-controls="editType" role="tab" data-toggle="tab">编辑/删除分类</a></li>
                </ul>

                <!-- Tab panes -->
                <div class="tab-content">
                    <!-- 添加板块 -->
                    <div role="tabpanel" class="tab-pane active" id="createTags">
                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">板块名</label>
                            <div class="col-sm-11">
                                <input type="text" class="form-control manage-item" id="tagsName" name="tagsName">
                                <button type="button" class="btn btn-default manage-item" onclick="createTag()">提交</button>
                            </div>
                        </div>

                    </div>

                    <!-- 添加分类 -->
                    <div role="tabpanel" class="tab-pane" id="createTypes">

                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">板块</label>
                            <div class="col-sm-11">
                                <select id="tagId" class="form-control manage-item" style="width: 40%;">
                                    <option value="-1">请选择</option>
                                    <c:forEach items="${types.tags}" var="tag">
                                        <option value="${tag.tagsId}">${tag.tagsName}</option>
                                    </c:forEach>

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">分类名</label>
                            <div class="col-sm-11">
                                <input type="text" class="form-control manage-item" id="typeName" name="typeName">
                                <button type="button" class="btn btn-default manage-item" onclick="createType()">提交</button>
                            </div>
                        </div>



                    </div>

                    <!-- 编辑/删除板块 -->
                    <div role="tabpanel" class="tab-pane" id="editTag">
                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">板块</label>
                            <div class="col-sm-11">
                                <select id="tagId2" class="form-control manage-item" style="width: 40%;">
                                    <option value="-1">请选择</option>
                                    <c:forEach items="${types.tags}" var="tag">
                                        <option value="${tag.tagsId}">${tag.tagsName}</option>
                                    </c:forEach>

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">板块名</label>
                            <div class="col-sm-11">
                                <input type="text" class="form-control manage-item" id="tagsName2">
                                <button type="button" class="btn btn-default manage-item" onclick="deleteTag()">删除</button>
                                <button type="button" class="btn btn-default manage-item" onclick="updateTag()">更改</button>
                            </div>
                        </div>


                    </div>


                    <!-- 编辑/删除分类 -->
                    <div role="tabpanel" class="tab-pane" id="editType">
                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">板块</label>
                            <div class="col-sm-5">
                                <select id="tagId3" class="form-control manage-item" style="width: 100%;">
                                    <option value="-1">请选择</option>
                                    <c:forEach items="${types.tags}" var="tag">
                                        <option value="${tag.tagsId}">${tag.tagsName}</option>
                                    </c:forEach>

                                </select>
                            </div>
                            <label class="col-sm-1 control-label manage-item">分类</label>
                            <div class="col-sm-5">
                                <select id="typeId" class="form-control manage-item" style="width: 100%;">

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label manage-item">分类名</label>
                            <div class="col-sm-11">
                                <input type="text" class="form-control manage-item" id="typeName2">
                                <button type="button" class="btn btn-default manage-item" onclick="deleteType()">删除</button>
                                <button type="button" class="btn btn-default manage-item" onclick="updateType()">更改</button>
                            </div>
                        </div>

                    </div>

                </div>

            </div>

        </div>

    </div>


</div>
<script type="text/javascript">
    function createTag() {
        var tagname = $("#tagsName").val();
        $.post("${pageContext.request.contextPath}/api/admin/tag/add",{"tagName":tagname},function (data) {
            if (data.success==1){
                alert("添加成功！");
                window.location.reload();
            } else{
                alert("添加失败！");
            }
        })

    }

    function createType() {
        var tagid = $("#tagId").val();
        var typename = $("#typeName").val();
        $.post("${pageContext.request.contextPath}/api/admin/type/add",{"tagId":tagid,"typeName":typename},function (data) {
            if (data.success==1){
                alert("添加成功！");
                window.location.reload();
            } else{
                alert("添加失败！");
            }
        })

    }

    function updateTag() {
        var tagid = $("#tagId2").val();
        var tagname = $("#tagsName2").val();
        $.post("${pageContext.request.contextPath}/api/admin/tag/update",{"tagId":tagid,"tagName":tagname},
        function (data) {
            if (data.success==1){
                alert("修改成功！");
                window.location.reload();
            } else{
                alert("修改失败！");
            }
        })

    }

    function deleteTag() {
        var tagid = $("#tagId2").val();
        $.post("${pageContext.request.contextPath}/api/admin/tag/delete",{"tagId":tagid},function (data) {
            if (data.success==1){
                alert("删除成功！");
                window.location.reload();
            } else{
                alert("删除失败！");
            }
        })

    }

    
    function updateType() {
        var tagid = $("#tagId3").val();
        var typeid = $("#typeId").val();
        var typename = $("#typeName2").val();
        $.post("${pageContext.request.contextPath}/api/admin/type/update",
            {"tagId":tagid,"typeId":typeid,"typeName":typename},function (data) {
                if (data.success==1){
                    alert("修改成功！");
                    window.location.reload();
                } else{
                    alert("修改失败！");
                }
            })


        
    }
    
    function deleteType() {
        var typeid = $("#typeId").val();
        $.post("${pageContext.request.contextPath}/api/admin/type/delete",{"typeId":typeid},function (data) {
            if (data.success==1){
                alert("删除成功！");
                window.location.reload();
            } else{
                alert("删除失败！");
            }
        })

    }

    $("#tagId3").change(function () {
        var tagid = $("#tagId3").val();
        var option="<option value=\"-1\">请选择</option>";
        $.post("${pageContext.request.contextPath}/api/admin/note/getType",{"tagId":tagid},function (data) {
            for (let i=0;i<data.length;i++){
                option+="<option value='"+data[i].typeid+"'>"+data[i].typename+"</option>";
            }
            $("#typeId").html(option);

        });

    });
</script>
</body>
</html>
